import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import session from 'express-session';
import ms from 'ms';
import redis from 'redis';
import connectRedis from 'connect-redis';

import { AppModule } from './app.module';

const RedisStore = connectRedis(session);
const redisClient = redis.createClient();

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  app.use(
    session({
      name: 'MY_SESSION',
      secret: `some-random-secret-here`,
      resave: true,
      saveUninitialized: false,
      rolling: true,
      cookie: {
        secure: false,
        maxAge: ms('5m'),
      },
      store: new RedisStore({
        host: 'localhost',
        port: 6379,
        serializer: JSON,
        client: redisClient,
        ttl: ms('5.5m'),
      }),
    }),
  );
  await app.listen(4000);
}
bootstrap();
